const net = require("./net2");
const http = require("./http");

var InfiniteLoad = (function () {
    function InfiniteLoad(config) {
        this.config = config;
        this.page = 1;
        this.total_page = 2;
        this.loading = false;
        this.url = config.url;
        this.data = config.data;
        this.requireLogin = config.requireLogin;

    }

    InfiniteLoad.prototype.load = function (cb) {
        if (this.loading || this.page > this.total_page) {
            cb(null, this.page, this.total_page);
            return;
        }
        console.log(this);
        var obj = this;
        var d = { page: this.page };
        if (this.data) {
            for (var k in this.data) {
                d[k] = this.data[k];
            }
        }


        http.get(obj.url, {
            data: d
        }).then(res => {
            obj.loading = false;
            obj.page = obj.page + 1;
            if (res.data.total) {
                obj.total_page = res.data.total;
            }

            if (cb) {
                cb(res, obj.page, obj.total_page);
            }
        })
    }

    InfiniteLoad.prototype.hasData = function () {
        console.log(this.page, this.total_page);
        return this.page < this.total_page;
    }


    InfiniteLoad.prototype.init = function () {
        this.page = 1;
        this.total_page = 2;
        this.loading = false;
    }

    InfiniteLoad.prototype.setUrl = function (url) {
        this.url = url;
    }
    InfiniteLoad.prototype.isLoading = function () {
        return this.loading;
    }
    return InfiniteLoad;
})();

module.exports = InfiniteLoad;